# 导包
import time
from pprint import pprint

import selenium.common.exceptions
from selenium import webdriver
from selenium.webdriver.common.by import By

# 定义一个定位方式类
class TestCase:
    def __init__(self):#初始化对象属性
        self.driver = webdriver.Chrome()
        self.driver.get("https://www.baidu.com/")
    def clear(self):
        self.driver.get("https://www.baidu.com/")
        # 通过id属性定位
        time.sleep(3)
        kw = self.driver.find_element(By.ID, "kw")
        kw.send_keys("天气")    #在输入框输入天气

        time.sleep(2)
        kw.clear()

        time.sleep(2)
        kw.send_keys("python自动化测试")

    # 获取标签HTML属性
    def get_attribute(self):
        self.driver.get('https://www.baidu.com')
        kw = self.driver.find_element(By.ID, 'kw')
        print(kw.get_attribute('outerHTML'))
        print(kw.get_attribute('id'))
        print(kw.get_attribute('class'))
        print(kw.get_attribute('name'))
        print(kw.get_attribute('autocomplete'))

    # 判断标签是否在页面中显示,返回结果为True,False
    def is_displayed(self):
        self.driver.get('https://www.baidu.com')
        ie = self.driver.find_element(By.XPATH, "//*[@id='form']/input[1]")
        print(ie.is_displayed())

        kw = self.driver.find_element(By.ID, 'kw')
        print(kw.is_displayed())

    # 判断元素是否可用,返回结果为True,False
    def is_enabled(self):
        self.driver.get('https://sahitest.com/demo/clicks.htm')
        bt1 = self.driver.find_element(By.CSS_SELECTOR, 'body > form > input[type=button]:nth-child(16)')
        print(bt1.is_enabled())

        bt2 = self.driver.find_element(By.XPATH, 'html/body/form/input[3]')
        print(bt2.is_enabled())



# 调用函数
if __name__ == '__main__':
    case = TestCase()
    case.clear()
    case.get_attribute()
    case.is_displayed()
    case.is_enabled()

    time.sleep(2)
    case.driver.quit()

